
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<title>DataSource.Get</title>

<style type="text/css">
/*margin and padding on body element
  can introduce errors in determining
  element position and are not recommended;
  we turn them off as a foundation for YUI
  CSS treatments. */
body {
	margin:0;
	padding:0;
}
</style>

<link type="text/css" rel="stylesheet" href="../../build/cssfonts/fonts-min.css" />
<script type="text/javascript" src="../../build/yui/yui-min.js"></script>


<!--begin custom header content for this example-->
<style type="text/css">
/* custom styles for this example */
#demo .output {margin-bottom:1em; padding:10px; border:1px solid #D9D9D9;}
</style>

<!--end custom header content for this example-->

</head>

<body class="yui3-skin-sam  yui-skin-sam">

<h1>DataSource.Get</h1>

<div class="exampleIntro">
	DataSource.Get uses the Get Utility to retrieve data, even cross-domain resources, via a dynamically created script node. A <a href="http://developer.yahoo.com/yui/3/dataschema/">DataSchema</a> plugin is used to normalize incoming data into a known format for consistency of usage by other components. Please note that your data resource must support a callback mechanism, which is a function wrapper around the returned data. The name of the callback function is passed to the resource via a query string parameter defined by the DataSource.Get attribute <code>scriptCallbackParam</code>.
			
</div>

<!--BEGIN SOURCE CODE FOR EXAMPLE =============================== -->

<form id="demo">
    <h4>JSON</h4>
    <h6>Data</h6>
    <pre>
{
    "query":{
        "count":"10",
        "created":"2009-06-04T05:15:06Z",
        "lang":"en-US",
        "updated":"2009-06-04T05:15:06Z",
        "uri":"http://query.yahooapis.com/v1/yql?q=...query%3D%22pizza%22",
        ...
        "results":{
            "result":[
                {
                    ...
                    "dispurl":"www.&lt;b&gt;pizzahut.com&lt;\/b&gt;",
                    "size":"16217",
                    "title":"&lt;b&gt;Pizza&lt;\/b&gt; Hut",
                    "url":"http://www.pizzahut.com/"
                },
                {
                    ...
                    "dispurl":"www.&lt;b&gt;dominos.com&lt;\/b&gt;",
                    "size":"29739",
                    "title":"Domino's &lt;b&gt;Pizza&lt;\/b&gt;",
                    "url":"http://www.dominos.com/"
                },
                {
                    ...
                    "dispurl":"&lt;b&gt;mobile.papajohns.com&lt;\/b&gt;",
                    "size":"2315",
                    "title":"Papa John's Mobile Web",
                    "url":"https://mobile.papajohns.com/"
                },
                ...
            ]
        }
    }
}
    </pre>

    <h6>Schema</h6>
    <pre>
{
    resultListLocator: "query.results.result",
    resultFields: ["title"]
}
    </pre>

    <h6>Normalized data</h6>
    <input type="button" id="demo_json" value="Retrieve data =>">
    <div id="demo_output_json" class="output"></div>
</form>

<script type="text/javascript">
YUI().use("dump", "node", "datasource-get", "datasource-jsonschema", "datasource-xmlschema", function (Y) {
    var myDataSource = new Y.DataSource.Get({source:"http://query.yahooapis.com/v1/public/yql?format=json&"}),
        myCallback = {
            success: function(e){
                Y.one("#demo_output_json").setContent(Y.dump(e.response).replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;"));
            },
            failure: function(e){
                Y.one("#demo_output_json").setContent("Could not retrieve data: " + e.error.message);
            }
        };

    myDataSource.plug(Y.Plugin.DataSourceJSONSchema, {
        schema: {
            resultListLocator: "query.results.result",
            resultFields: ["title"]
        }
    });

    Y.on("click", function(e){
        myDataSource.sendRequest({
            request:"q=select%20*%20from%20search.web%20where%20query%3D%22pizza%22",
            callback:myCallback
        });
    }, "#demo_json");
});
</script>

<!--END SOURCE CODE FOR EXAMPLE =============================== -->

</body>
</html>
